﻿using System.Web.UI.WebControls.WebParts;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    class WebPartHelper
    {
        /// <summary>
        /// Check of a ESPConfigWebPart for a certain Search Group ID is avialable on the page.
        ///
        /// </summary>
        /// <param name="wpm"></param>
        /// <param name="ESPSearchGroupID"></param>
        /// <returns>0 if a ESPConfigWebPart is on the page, -1 if none is available or > 0 if more then one is avaiable</returns>
        public static int CheckForESPConfigWebPartTypeOnPage(WebPartManager wpm, ESPSearchGroupEnum ESPSearchGroupID)
        {
            int found = -1;
            if (wpm != null)
            {
                if (wpm.DisplayMode != WebPartManager.BrowseDisplayMode)
                {
                    WebPartZoneCollection zones = wpm.Zones;
                    foreach (WebPartZone zone in zones)
                    {
                        WebPartCollection webparts = zone.WebParts;
                        foreach (WebPart webpart in webparts)
                        {
                            if (webpart is ESPSearchConfig)
                            {
                                ESPSearchConfig espConfig = webpart as ESPSearchConfig;

                                if (espConfig.ESPSearchGroupID == ESPSearchGroupID)
                                {
                                    found += 1;
                                }
                            }
                        }
                    }
                }
            }
            return found;
        }
    }
}
